package main

import (
	"sync"
	"net/http"
	"log"
	"fmt"
)

var mu sync.Mutex
var count int

func main()  {
	http.HandleFunc("/count", counter)
	http.HandleFunc("/info", info)
	http.HandleFunc("/", enter)
	log.Fatal(http.ListenAndServe("localhost:8000", nil))
}

func enter(w http.ResponseWriter, r *http.Request)  {
	mu.Lock()
	count++
	mu.Unlock()
	fmt.Fprintf(w, "URL.PATH = %q\n", r.URL.Path)
}

func counter(w http.ResponseWriter, r *http.Request)  {
	mu.Lock()
	fmt.Fprintf(w, "count %d\n", count)
	mu.Unlock()
}

func info(w http.ResponseWriter, r *http.Request)  {
	fmt.Fprintf(w, "%s %s %s\n", r.Method, r.URL, r.Proto)
	for k,v:=range r.Header{
		fmt.Fprintf(w, "Header[%q] = %q\n", k, v)
	}
	fmt.Fprintf(w, "Host = %q\n", r.Host)
	fmt.Fprintf(w, "RemoteAddr = %q\n", r.RemoteAddr)
	if err:=r.ParseForm();err!=nil{
		log.Print(err)
	}
	for k,v:=range r.Form{
		fmt.Fprintf(w, "Form[%q] = %q\n", k,v)
	}
	//GET /info HTTP/1.1
	//Header["Cookie"] = ["Phpstorm-749c0e3e=993527b0-df5c-474d-ac33-05ae45cb43ad; PHPSESSID=04dbvkphgppg02cuch692po9s3; imgUrl=%2Fadmin%2Ffile%2Findex; prevUrl=%2Fadmin%2Fshop%2Fproduct%2Findex%2Findex.html"]
	//Header["Connection"] = ["keep-alive"]
	//Header["Upgrade-Insecure-Requests"] = ["1"]
	//Header["User-Agent"] = ["Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"]
	//Header["Accept"] = ["text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"]
	//Header["Accept-Encoding"] = ["gzip, deflate, br"]
	//Header["Accept-Language"] = ["zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7"]
	//Host = "localhost:8000"
	//RemoteAddr = "127.0.0.1:50249"
}